package com.weiyi.weiman.mapper.user;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import user.UserBrowseHistory;

import java.util.Date;
import java.util.List;

public interface UserBrowseHistoryMapper extends BaseMapper<UserBrowseHistory> {

    @Select("SELECT * FROM wm_user_browse_history " +
            "WHERE user_id = #{userId} AND gwt_create >= NOW() - INTERVAL #{expire} DAY")
    List<UserBrowseHistory> getHistory(long userId, int expire);

    @Select("SELECT * FROM wm_user_browse_history WHERE gwt_create < NOW() - INTERVAL #{expire} DAY")
    List<UserBrowseHistory> getExpireHistory(int expire);

    @Update("INSERT INTO wm_user_browse_history (user_id,comics_id) VALUES (#{userId},#{comicsId}) " +
            "ON DUPLICATE KEY UPDATE chapter_id = #{chapterId} AND gwt_create = #{now}")
    void insertAndUpdate(Long userId, Long comicsId, Long chapterId, Date now);

}
